package com.solo.pulldown;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.solo.pulldown.PullDownView.OnPullDownListener;

public class PullDownActivity extends Activity implements OnPullDownListener, OnItemClickListener{

	private static final int WHAT_DID_LOAD_DATA = 0;
	private static final int WHAT_DID_REFRESH = 1;
	private static final int WHAT_DID_MORE = 2;

	private ListView mListView;
	private ArrayAdapter<String> mAdapter;

	private PullDownView mPullDownView;
	private List<String> mStrings = new ArrayList<String>();
	private List<Map<String,Object>> strings = new ArrayList<Map<String,Object>>();
	private Map<String,Object> mStrings2=new HashMap<String, Object>();
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	private List<String> title = new ArrayList<String>();
	private List<String> note = new ArrayList<String>();
	private List<String> img = new ArrayList<String>();
	private String[] strsTitle={
			"路飞的船员","路飞的船员",
			"路飞的船员","路飞的船员",
			"路飞的船员","路飞的船员",
			"路飞的船员","路飞的船员",
			"路飞的船员"
	};
	private String[] strsTime={
			"09:08","19:07","09:10",
			"09:16","09:13","09:16",
			"09:16","09:16","09:16"
	};
	private String[] strsBrowse={
			"1","2","3","4",
			"5","6","7","8",
			"9"
	};
	private int[] strsHead={
		R.drawable.img_1,
		R.drawable.img_2,
		R.drawable.img_3,
		R.drawable.img_4,
		R.drawable.img_5,
		R.drawable.img_6,
		R.drawable.img_7,
		R.drawable.img_8,
		R.drawable.img_9,
	};
	private int[] strsPhoto={
			R.drawable.img_1,
			R.drawable.img_2,
			R.drawable.img_3,
			R.drawable.img_4,
			R.drawable.img_5,
			R.drawable.img_6,
			R.drawable.img_7,
			R.drawable.img_8,
			R.drawable.img_9,
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pulldown);
		
		/*
		 * 1.使用PullDownView
		 * 2.设置OnPullDownListener
		 * 3.从mPullDownView里面获取ListView
		 */
		mPullDownView = (PullDownView) findViewById(R.id.pull_down_view);
		mPullDownView.setOnPullDownListener(this);
		mListView = mPullDownView.getListView();
		
		mListView.setOnItemClickListener(this);
		loadData();//第一次打开，读取本地数据，显示在页面上
		 
//		mAdapter = new ArrayAdapter<String>(this, R.layout.pulldown_item, mStrings);
//		SimpleAdapter mAdapter=new SimpleAdapter(this,list,R.layout.pulldown_item2, new String[]{"title","note","img"}, new int[]{R.id.pulldown_item2_text,R.id.pulldown_item2_text2,R.id.pulldown_item2_image});
        mListView.setAdapter(new MyAdapter(strsTitle, strsTime, strsBrowse, strsHead, strsPhoto, this));
        
        mPullDownView.enableAutoFetchMore(true, 1);
       
	}
	private List<Map<String, Object>> getData() {
        
		String[] title={
				"路飞的船员","路飞的船员",
				"路飞的船员","路飞的船员",
				"路飞的船员","路飞的船员",
				"路飞的船员","路飞的船员",
				"路飞的船员"
		};
		String[] note={
				"蒙奇D.路飞","罗罗诺亚.佐罗",
				"黑足.香吉士","娜米",
				"罗宾酱","托尼托尼.乔巴",
				"弗兰奇","布鲁克",
				"乌索普"
				
		};
		int[] img={
				R.drawable.arrow,R.drawable.arrow,
				R.drawable.arrow,R.drawable.arrow,
				R.drawable.arrow,R.drawable.arrow,
				R.drawable.arrow,R.drawable.arrow,
				R.drawable.arrow
		};
        Map<String, Object> map = new HashMap<String, Object>();
        for(int i=0;i<title.length;i++)
        {
        	map.put("title", title[i]);
        	map.put("note", note[i]);
        	map.put("img", img[i]);
        	list.add(map);
        }
         
        return list;
    }
	private void loadData(){
		new Thread(new Runnable() {
			//第一次进去刷新页面
			@Override
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
//				for (HashMap<String,Object> body : mStrings2) {
//					strings.add(body);
//				}
				getData();//获取数据实体
				Message msg = mUIHandler.obtainMessage(WHAT_DID_LOAD_DATA);
				msg.obj = strings;
				msg.sendToTarget();
			}
		}).start();
	}


	@Override
	public void onRefresh() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}				
				Message msg = mUIHandler.obtainMessage(WHAT_DID_REFRESH);
				msg.obj = "After refresh " + System.currentTimeMillis();
				msg.sendToTarget();
			}
		}).start();
	}
	
	@Override
	public void onMore() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}				
				Message msg = mUIHandler.obtainMessage(WHAT_DID_MORE);
				msg.obj = "After more " + System.currentTimeMillis();
				msg.sendToTarget();
			}
		}).start();
	}
	
	private Handler mUIHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case WHAT_DID_LOAD_DATA:{
					if(msg.obj != null){
						List<String> strings = (List<String>) msg.obj;
						if(!strings.isEmpty()){
							mStrings.addAll(strings);
							mAdapter.notifyDataSetChanged();
						}
					}
					// 诉它数据加载完毕;
					mPullDownView.notifyDidLoad();
					break;
				}
				case WHAT_DID_REFRESH :{
					String body = (String) msg.obj;
					mStrings.add(0, body);
					mAdapter.notifyDataSetChanged();
					// 告诉它更新完毕
					mPullDownView.notifyDidRefresh();
					break;
				}
				
				case WHAT_DID_MORE:{
					String body = (String) msg.obj;
					mStrings.add(body);
					mAdapter.notifyDataSetChanged();
					// 告诉它获取更多完毕
					mPullDownView.notifyDidMore();
					break;
				}
			}
			
		}
		
	};

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Toast.makeText(this, "啊，你点中我了 " + position, Toast.LENGTH_SHORT).show();
	}
	
	
	
	// 模拟数据
	private String[] mStringArray = {
            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
            "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese"
    };
	class MyAdapter extends BaseAdapter{
		private String[] strsTitle,strsTime,strsBrowse;
		private int[] strsPhoto,strsHead;
		private Context mContext;
		private LayoutInflater inflate;
		public MyAdapter(String[] strsTitle,String[] strsTime,String[] strsBrowse,int[] strsHead,int[] strsPhoto,Context context){
			this.strsTitle=strsTitle;
			this.strsTime=strsTime;
			this.strsBrowse=strsBrowse;
			this.strsHead=strsHead;
			this.strsPhoto=strsPhoto;
			this.mContext=context;
			inflate=(LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return strsTime.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView=inflate.inflate(R.layout.pulldown_item2, null);
			TextView title=(TextView) convertView.findViewById(R.id.pulldown_item2_title);
			TextView time=(TextView) convertView.findViewById(R.id.pulldown_item2_time);
			TextView browse=(TextView) convertView.findViewById(R.id.pulldown_item2_browse);
			ImageView headIcon=(ImageView)convertView. findViewById(R.id.pulldown_item2_image1);
			ImageView photoIcon=(ImageView)convertView. findViewById(R.id.pulldown_item2_image2);
			title.setText(strsTitle[position]);
			time.setText(strsTime[position]);
			browse.setText(strsBrowse[position]);
			headIcon.setBackgroundResource(strsHead[position]);
			photoIcon.setBackgroundResource(strsPhoto[position]);
			return convertView;
		}
		
	}

}
